class KramdownFilter < TextFilter
  description_file File.dirname(__FILE__) + "/../markdown.html"

  def options
    c = Radiant::Config
    # set default options for the kramdown library
    # the type conversion stuff is due to kramdown having strict rules
    # about the types given as option values but Radiant::Config
    # stores all values as simple strings.
    o = {
      :auto_ids                   => { :default => true,               :type => :boolean },
      :auto_id_prefix             => { :default => '',                 :type => :string  },
      :parse_block_html           => { :default => false,              :type => :boolean },
      :parse_span_html            => { :default => true,               :type => :boolean },
      :footnote_nr                => { :default => 1,                  :type => :integer },
      :coderay_wrap               => { :default => :div,               :type => :symbol  },
      :coderay_line_numbers       => { :default => :inline,            :type => :symbol  },
      :coderay_line_number_start  => { :default => 1,                  :type => :integer },
      :coderay_tab_width          => { :default => 0,                  :type => :integer },
      :coderay_bold_every         => { :default => 10,                 :type => :integer },
      :coderay_css                => { :default => :style,             :type => :symbol  },
      :entity_output              => { :default => :as_char,           :type => :symbol  },
      :toc_levels                 => { :default => [1, 2, 3, 4, 5, 6], :type => :array   }
    }
    # overwrite defaults with user set values
    o.keys.each { |key|
      if c["kramdown.#{key.to_s}"]
        case o[key][:type]
        when :boolean
          boolean = c["kramdown.#{key.to_s}"] == "true" ? true : false
          o[key][:default] = boolean
        when :integer
          o[key][:default] = c["kramdown.#{key.to_s}"].to_i
        when :string
          o[key][:default] = c["kramdown.#{key.to_s}"]
        when :symbol
          symbol = c["kramdown.#{key.to_s}"] != "nil" ? c["kramdown.#{key.to_s}"].to_sym : nil
          o[key][:default] = symbol
        end
      end
    }
    return o
  end

  def filter(text)
    o = self.options
    Kramdown::Document.new(text,
    {
      :auto_ids                   => o[:auto_ids][:default],
      :auto_id_prefix             => o[:auto_id_prefix][:default],
      :parse_block_html           => o[:parse_block_html][:default],
      :parse_span_html            => o[:parse_span_html][:default],
      :footnote_nr                => o[:footnote_nr][:default],
      :coderay_wrap               => o[:coderay_wrap][:default],
      :coderay_line_numbers       => o[:coderay_line_numbers][:default],
      :coderay_line_number_start  => o[:coderay_line_number_start][:default],
      :coderay_tab_width          => o[:coderay_tab_width][:default],
      :coderay_bold_every         => o[:coderay_bold_every][:default],
      :coderay_css                => o[:coderay_css][:default],
      :entity_output              => o[:entity_output][:default],
      :toc_levels                 => o[:toc_levels][:default]
    }).to_html
  end
end
